﻿using System;
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class User
    {
        private string[,] _users;
        private MySqlConnection conn;


        public bool isOnline(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            bool online = false;

            if (this._users[id - 1,3] == "1")
                online = true;

            return online;
        }

        public void setOnlineState(int id, bool setOnline)
        {
            if (setOnline)
            {
                Query.UpdateQuery(conn, "users", "online", "1", "userid=" + id);
            }
            else
            {
                Query.UpdateQuery(conn, "users", "online", "0", "userid=" + id);
            }
        }

        public void banPlayer(int id, string reason)
        {
            string[] columns = new string[2];
            columns[0] = "banreason";
            columns[1] = "ban";

            string[] values = new string[2];
            values[0] = reason;
            values[1] = "1";

            Query.UpdateQuery(conn,"users",columns,values,"userid=" + id);
            
        }

        public void unbanPlayer(int id)
        {
            string[] columns = new string[2];
            columns[0] = "banreason";
            columns[1] = "ban";

            string[] values = new string[2];
            values[0] = "";
            values[1] = "0";

            Query.UpdateQuery(conn, "users", columns, values, "userid=" + id);
        }

        public bool doesExists(string name, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            bool exists = false;

            for (int i = 0; i < _users.GetLength(0); i++)
            {
                if (_users[i, 1] == name)
                    exists = true;
                break;
            }

            return exists;
        }

        public bool rightInfo(string name, string password, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            bool rightinfo = false;

            for (int i = 0; i < _users.GetLength(0); i++)
            {
                if (_users[i, 1] == name && _users[i, 2] == password)
                    rightinfo = true;
                break;
            }

            return rightinfo;
        }

        public bool canLogin(string name, string password, bool refresh = false)
        {
            bool can = false;
            if(this.doesExists(name,true) && this.rightInfo(name,password,true))
                can = true;
            return can;
        }

        public void login(int id)
        {
            this.setOnlineState(id, true);
        }

        public void setSilk(int id, int newCount,bool prem = false)
        {
            if (!prem)
            {
                Query.UpdateQuery(conn, "users", "silk", newCount.ToString(), "userid=" + id);
            }
            else
            {
                Query.UpdateQuery(conn, "users", "premsilk", newCount.ToString(), "userid=" + id);
            }

        }

        public int getSilk(int id, bool prem = false,bool refresh = false)
        {
            if (refresh)
                this.refresh();

            if (!prem)
            {
                return Other.toInt(_users[id - 1, 6]);
            }
            else
            {
                return Other.toInt(_users[id - 1, 9]);
            }
        }

        public void setGold(int id, int newCount)
        {
            Query.UpdateQuery(conn, "users", "gold", newCount.ToString(), "userid=" + id);
        }

        public int getGold(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return Other.toInt(_users[id -1, 7]);
        }

        public int getJob(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return Other.toInt(_users[id - 1, 10]);
        }

        public void setJob(int id, int newJob)
        {
            Query.UpdateQuery(conn, "users", "jobtype", newJob.ToString(), "userid=" + id);
        }

        public void setWarehouseSlots(int id, int newSlotsCount)
        {
            Query.UpdateQuery(conn, "users", "warehouse_slots", newSlotsCount.ToString(), "userid=" + id);
        }

        public int getWarehouseSlots(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return Other.toInt(_users[id - 1, 11]);
        }

        public int getCheckInfo(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return Other.toInt(_users[id - 1, 8]);
        }

        public string getUserNameById(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return (_users[id - 1,1]);
        }

        public int getIdByName(string name)
        {
            int id;

            for (int i = 0; i < _users.GetLength(0); i++)
            {
                if (_users[i, 2] == name)
                {
                    Int32.TryParse(_users[i, 0], out id);
                    return id;
                }
            }

            return 0;
        }

        public void refresh()
        {
            string[] columns = new string[1];
            columns[0] = "*";

            _users = Query.SelectQuery(conn, "users", columns);
        }

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public string[,] Users
        {
            get { return this._users; }
        }
    }
}
